\documentclass[12pt,oneside]{amsart}
\usepackage{hyperref}
\begin{document}
\title{\bfseries behave.R (r123)}
\author{Jason Pol\'ak}
\date{\today}
\maketitle

\tableofcontents

\section{Introduction}

The {\tt behave.R} script calculates various behavioural statistics from a raw $(x,y)$ data files with surface area data. It uses the R programming environment to batch process all data files in a directory very quickly.

\section{Set Up}

Go to {\tt http://www.r-project.org/} and click on download in the menu on the left, and download the appropriate version of R for your operating system. 

Make sure you have the files:

\begin{enumerate}
\item behave.R
\item 2020-12-12-907.csv
\end{enumerate}

Open the {\tt behave.R} program and the first section of it after the license statement will look like this:

\begin{verbatim}
#Paramters
#Tweak these values according to experiment

# Analysis Parameters
autorun = TRUE
show_mouse_number = TRUE
mouse_number_format_remove = "....-..-..-"
ftype = ","

# Data Parameters
meta_data_length = 17
start_time = 0
total_time = 1500

# Rearing
r_minimum_surface_area_diff = 25
r_samples_used = 10

# Time In Center
c_distance_from_edge = 3
\end{verbatim}

These basic parameters can be modified according to your experiment and data:

\begin{enumerate}

\item autorun (TRUE,FALSE): whether to process the data. Leave as true unless you want to debug the program or run individual functions

\item show\_mouse\_number (TRUE,FALSE): whether to include the species id number with the output. Note that by default the file must be in the format {\tt xxxx-yy-zz-mnumber.csv} in order for this to work, but the format can be changed by modifying the
{\tt mouse\_number\_format\_remove} parameter

\item mouse\_number\_format\_remove: the format of the filename in a regular expression

\item ftype (``," , ``." ,...) the separator that is used for the data. Default is "," for comma separated.

\item meta\_data\_length (0,1,2,3...): how many lines to skip in the data file. Default is 17 for Ethovision's default

\item start\_time(0,1,2,...): number of samples to start at. If you have five samples per second (default), then specify {\tt start\_time$=$1000} to get 200 seconds, for instance

\item total\_time (0,1,2,...): number of samples to use after the start time

\end{enumerate}

\subsection{Rearing}

Suppose the current timepoint is $k$. The rearing formula as used by Ethovision and thus by this script is the following:

\begin{equation*}
p = \frac{S(k) - (\sum_{i=k-1+N}^k S(i))/N}{(\sum_{i=k-1+N}^k S(i))/N}
\end{equation*}

Where $N$ is the samples to average over, and $S(x)$ is the surface area at time $x$. This formula thus takes the current surface area and calculates relative change based on the average surface area from sample $k - N + 1$ to sample $k$ for timepoint $k$.

This gives a proportion $p$ change. Multiply by this by $100$ to get a percentage. The parameter {\tt r\_minimum\_surface\_area\_diff} is the minimum of this percentage that needs to be reached for the rearing frequency to be incremented by unity.

The parameter {\tt r\_samples\_used} is $N$. The defaults are $25$ and $10$ respectively. The Ethovision manual states that these values are good for rats. Use at your own risk!

\subsection{Time In Center}

The "center" is defined as a square with the same center as the arena with a edge $x$ cm from the edge of the area. The default {\tt c\_distance\_from\_edge} is $3$.

\section{Testing Your Setup}
Open the {\tt behave.R} script and use the following paramters:

\begin{verbatim}
#Paramters
#Tweak these values according to experiment

# Analysis Parameters
autorun = TRUE
show_mouse_number = TRUE
mouse_number_format_remove = "....-..-..-"
ftype = ","

# Data Parameters
meta_data_length = 17
start_time = 0
total_time = 1500

# Rearing
r_minimum_surface_area_diff = 25
r_samples_used = 2

# Time In Center
c_distance_from_edge = 3
\end{verbatim}

Start R, and type:

\begin{verbatim}
setwd(directory)
\end{verbatim}

Where {\tt directory} is the directory that you have stored the\\ {\tt 2020-12-12-907.csv} test file. Type:

\begin{verbatim}
source("path/to/behave.R")
\end{verbatim}

Modifying the path as needed. In the directory of the test data file the, there will be a file called "opf-data.txt". Open it and compare it to this:

\begin{verbatim}
mouse_id	ave_sa	rearing_freq	total_distance	prop_in_center
907	83.4	2	32.0327238415415	0.5
\end{verbatim}

If the numbers are the same, then everything works. Now you can navigate\footnote{Or use a script to do this.} with the {\tt setwd()} command to the directory of the actual data files and retype the {\tt source} command to get the data. Remember to tweak the parameters.

\end{document}
